clear;clc;
client=EquipmentControl.Comm.Client('192.168.1.253', 54321);client.start;
%%
getSession;
sess.connect_device
zi=sess.getDevice('dev3567');
%%
zi.lock('0'); pause(3);
freqCloseLoop = getLaserFrequencyList(client, 7200, 0.5);
sess.addData('Close', freqCloseLoop, 'closeloop7200pts_rate0p5');

%%
zi.unLock('0'); pause(3);
freqOpenLoop = getLaserFrequencyList(client, 7200, 0.1);
sess.addData('Open', freqOpenLoop, 'openloop7200pts');
%%
figure;
subplot(2, 2, 1)
plot(freqOpenLoop-mean(freqOpenLoop), 'bo-');
title(sprintf('MeanFreq = %9.6f MHz', mean(freqOpenLoop)/1e6));
subplot(2, 2, 2)
histogram(freqOpenLoop-mean(freqOpenLoop));

subplot(2, 2, 3)
plot(freqCloseLoop-mean(freqCloseLoop), 'bo-');
title(sprintf('MeanFreq = %9.6f MHz', mean(freqCloseLoop)/1e6));
subplot(2, 2, 4)
histogram(freqCloseLoop-mean(freqCloseLoop));


%%
function freqList = getLaserFrequencyList(client, nPt, dt)
    f=figure('Name', 'Monitor Laser Frequency');
    freqList = null(1, nPt);
    for k=1:nPt
        pause(dt);
        freqList(k) = client.GetFrequency();
        meanFreq = mean(freqList);
        plot(freqList-meanFreq, 'bo-');
        title(sprintf('MeanFreq = %9.6f MHz', meanFreq/1e6));
        ylabel('Frequency shift (MHz)');
    end
    close(f);
end
